import {exportToExcel} from "./export2Excel"

const th_td_style = {
    height: 40,
    // width:'60px',
}
const img = "https://ss1.bdstatic.com/70cFuXSh_Q1YnxGkpoWK1HF6hhy/it/u=2801998497,4036145562&fm=27&gp=0.jpg"
const export2Excel = (theadData, tbodyData, dataname) => {

    let re = /http/ // 字符串中包含http,则默认为图片地址
    let th_len = theadData.length // 表头的长度
    let tb_len = tbodyData.length // 记录条数
    let width = 40 // 设置图片大小
    let height = 60
    // 横向图片的高度
    let width1 = 40 * theadData.length // 设置图片大小
    let height1 = 40 * 6

    // 添加表头信息
    let thead = ''
    thead = '<thead><tr>'
    for (let i = 0; i < th_len; i++) {
        thead += '<th>' + theadData[i] + '</th>'
    }
    thead += '</tr></thead>'

    // 添加每一行数据
    let tbody = '<tbody>'
    tbody += `<tr>`
    //width:' + width1 + 'px; height:' + height1 + 'px;
    tbody += `<td colspan=${th_len} style="height: '${height1}px'; text-align: center; vertical-align: middle"><div style="display:inline"><img src=${img} width=${width1} height=${height1}></div></td>`
    tbody += '</tr>'
    for (let i = 0; i < tb_len; i++) {
        tbody += '<tr>'
        let row = tbodyData[i] // 获取每一行数据

        for (let key in row) {
            if (re.test(row[key])) { // 如果为图片，则需要加div包住图片
                //
                tbody += '<td style="width:' + width + 'px; height:' + height + 'px; text-align: center; vertical-align: middle"><div style="display:inline"><img src=\'' + row[key] + '\' ' + ' ' + 'width=' + '\"' + width + '\"' + ' ' + 'height=' + '\"' + height + '\"' + '></div></td>'
            } else {
                tbody += '<td style="text-align:center">' + row[key] + '</td>'
            }
        }
        tbody += '</tr>'
    }
    tbody += '</tbody>'

    let table = thead + tbody

    // 导出表格
    exportToExcel(table, `${dataname}.xlsx`)
}

export {
    export2Excel
}
